package socilgirl.dell.mydemo.greendao.bean;

import com.google.gson.Gson;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;

import java.util.Arrays;
import java.util.List;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.converter.PropertyConverter;
import org.greenrobot.greendao.annotation.Generated;

/**
 * Created by hgk on 2018/4/2/002 16:36.
 */
@Entity
public class ShopEntity {
    @Id
    String shopId;
    String shopName;
    @Property
    @Convert(columnType = String.class, converter = ShopCoverter.class)
    ShopDetail shopDetail;
    @Property
    @Convert(columnType = String.class,converter = StringShopPic.class)
    List<String> shopPic;

    @Generated(hash = 749186195)
    public ShopEntity(String shopId, String shopName, ShopDetail shopDetail,
            List<String> shopPic) {
        this.shopId = shopId;
        this.shopName = shopName;
        this.shopDetail = shopDetail;
        this.shopPic = shopPic;
    }
    @Generated(hash = 253351364)
    public ShopEntity() {
    }
    public String getShopId() {
        return this.shopId;
    }
    public void setShopId(String shopId) {
        this.shopId = shopId;
    }
    public String getShopName() {
        return this.shopName;
    }
    public void setShopName(String shopName) {
        this.shopName = shopName;
    }
    public ShopDetail getShopDetail() {
        return this.shopDetail;
    }
    public void setShopDetail(ShopDetail shopDetail) {
        this.shopDetail = shopDetail;
    }
    public List<String> getShopPic() {
        return this.shopPic;
    }
    public void setShopPic(List<String> shopPic) {
        this.shopPic = shopPic;
    }

    //将商品的详细介绍信息添加到数据库中
    public static class ShopCoverter implements PropertyConverter<ShopDetail,String>{
        @Override
        public ShopDetail convertToEntityProperty(String databaseValue) {
            if (databaseValue==null) {
                return null;
            }
            return  new Gson().fromJson(databaseValue,ShopDetail.class);
        }

        @Override
        public String convertToDatabaseValue(ShopDetail entityProperty) {
            if (entityProperty == null) {
                return null;
            }
            return new Gson().toJson(entityProperty);
        }
    }

    //将商品图片集合添加到数据库中
    public static class StringShopPic implements PropertyConverter<List<String>,String> {
        @Override
        public List<String> convertToEntityProperty(String databaseValue) {
            if (databaseValue==null){
                return null;
            }
            List<String> list = Arrays.asList(databaseValue.split(","));
            return list;
        }

        @Override
        public String convertToDatabaseValue(List<String> entityProperty) {
            if (entityProperty == null) {
                return null;
            }else{
                StringBuilder sb = new StringBuilder();
                for (String link:entityProperty){
                    sb.append(link);
                    sb.append(",");
                }
                return sb.toString();
            }
        }
    }
}
